const express = require("express");
const app = express();
const path = require("path");
const url = require("url");
const vipLogin = require("./data/vip_login.json");
const adminLogin = require("./data/admin_login.json");
const cors = require("cors");

app.use(cors({ exposedHeaders: "*" }));
app.get("/login", (req, res) => {
  const user = url.parse(req.url, true).query.user;
  if (user === "admin") {
    res.send(adminLogin);
  } else {
    res.send(vipLogin);
  }
});

const adminPermissionTwo = require("./data/admin_permissionTwo.json");
const vipPermission = require("./data/vip_permission.json");
const adminPermissionThree = require("./data/admin_permissionThree.json");
const error = require("./data/error.json");
app.get("/permission", (req, res) => {
  // const user = url.parse(req.url, true).query.user;
  // if (user === "admin2") {
  //   res.send(adminPermissionTwo);
  // } else if (user === "admin3") {
  //   res.send(adminPermissionThree);
  // } else {
  //   res.send(vipPermission);
  // }
  if (req.query.user === "admin" || req.query.user === "user") {
    res.send(vipPermission);
  } else {
    res.send(error);
  }
});
app.get("/download", (req, res) => {
  const filePath = path.join(__dirname, "data", "/333.pdf");
  res.setHeader("Content-Type", "application/pdf");
  res.setHeader("Content-Disposition", 'attachment; filename="333.pdf"');
  /* CORS配置 */
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Expose-Headers", "*");
  res.sendFile(filePath);
});
app.listen(3300, () => {
  console.log("server is running on port 3300");
});
